﻿#pragma once

//#ifdef _WIN32
//#define WIN32_LEAN_AND_MEAN
//#include <windows.h>
//#include <winsock2.h>
//#include <ws2tcpip.h>
//#pragma comment(lib, "ws2_32.lib")
//#endif

//#include <httplib/httplib.h>
#include <iostream>
#include <QObject>

namespace httplib {
	class Server;
};


typedef std::function<void(const QByteArray& req, QByteArray& ans)> ServerFun;

struct ServerInter {
	QString name;
	ServerFun callback;	// 回调函数
	bool log;		// 是否记录日志
};
#define wcs_send Http::GetInstance()->SendPost
#define wcs_send_get Http::GetInstance()->SendGet
class Http
{
public:
	Http(const QString& wcsUrl = "http://127.0.0.1:8089");
	~Http();

	static Http* GetInstance();

	void SetWCSUrl(const QString& wcsUrl);
	bool Start(const int port, const QString& ip = "0.0.0.0");

	void Stop();
	/*type: 0表示post 1表示get*/
	bool RegisterInterface(const QString& path,const ServerInter& inter, const int type = 0);

	bool SendGet(const QString& path, QByteArray& ans);
	bool SendPost(const QString& path, QByteArray& ans, const QByteArray& body);


	//void get(const std::string& pattern, std::function<void(const httplib::Request&, httplib::Response&)> handler);
	//void post(const std::string& pattern, std::function<void(const httplib::Request&, httplib::Response&)> handler);


private:
	std::unique_ptr<httplib::Server> m_server; 
	QString m_wcsUrl;
};
